<?php
/*
* This plugin uses parts of JCE extension by Ryan Demmer.
* @copyright	Copyright (C) 2005 - 2011 Ryan Demmer. All rights reserved.
* @copyright	Copyright (C) 2003 - 2011, CKSource - Frederico Knabben. All rights reserved.
* @license		GNU/GPL
* CKEditor extension is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/
// no direct access
defined('_CKE_EXT') or die('Restricted access');
class JoomlalinksWeblinks extends JObject
{

		var $_option = 'com_weblinks';
		/**
		 * Constructor activating the default information of the class
		 *
		 * @access	protected
		 */
		function __construct($options = array())
		{
		}

		/**
		 * Returns a reference to a editor object
		 *
		 * This method must be invoked as:
		 * 		<pre>  $browser = &JContentEditor::getInstance();</pre>
		 *
		 * @access	public
		 * @return	JCE  The editor object.
		 * @since	1.5
		 */
		function & getInstance()
		{
				static $instance;

				if (!is_object($instance)) {
						$instance = new JoomlalinksWeblinks();
				}
				return $instance;
		}
		function getOption()
		{
				return $this->_option;
		}
		function getList()
		{
				$wf =& WFEditorPlugin::getInstance();

				if ($wf->checkAccess('weblinks', '1')) {
						return '<li id="index.php?option=com_weblinks&view=categories"><div class="tree-row"><div class="tree-image"></div><span class="folder weblink nolink"><a href="javascript:;">'.JText::_('WEBLINKS').'</a></span></div></li>';
				}
		}
		function getLinks($args)
		{
				$items = array();

				switch ($args->view) {
						// Get all WebLink categories
						default:
						case 'categories':
								$categories = WFLinkBrowser::getCategory('com_weblinks');
								foreach ($categories as $category) {
										$itemid = WFLinkBrowser::getItemId('com_weblinks', array('categories'=>null, 'category'=>$category->id));
										$items[] = array('id'=>'index.php?option=com_weblinks&view=category&id='.$category->id.$itemid, 'name'=>$category->title.' / '.$category->alias, 'class'=>'folder weblink');
								}
								break;
						// Get all links in the category
						case 'category':
								require_once (JPATH_SITE.DS.'includes'.DS.'application.php');
								require_once (JPATH_SITE.DS.'components'.DS.'com_weblinks'.DS.'helpers'.DS.'route.php');

								$weblinks = $this->getWebLinks($args->id);
								foreach ($weblinks as $weblink) {
										$items[] = array('id'=>WeblinksHelperRoute::getWeblinkRoute($weblink->id, $args->id), 'name'=>$weblink->title.' / '.$weblink->alias, 'class'=>'file');
								}
								break;
				}
				return $items;
		}
		function getWebLinks($id)
		{
				$db = &JFactory::getDBO();

				$query = 'SELECT title, id, alias'.' FROM #__weblinks'.' WHERE access = 1'.' AND catid = '.(int) $id.' ORDER BY title';

				$db->setQuery($query, 0);
				return $db->loadObjectList();
		}
}
?>
